package com.wy.tingshu.servlet.album;

import com.wy.tingshu.util.DBUtil;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * （a标签跳转过来的）
 * 专辑的发布/上线
 **/
@WebServlet("/studio/album/publish.do")
public class PublishDoServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //验证专辑是否属于当前用户，避免下线/发布别人的专辑
          //判断用户是谁，要能对上专辑里面的用户uid，才能执行发布专辑操作
        //TODO:验证当前登录用户是专辑的主人，才能进行该操作
        String aid=req.getParameter("aid");
        try (Connection c= DBUtil.connection()){
            String sql="update albums set state = 2 where aid = ?";//专辑表主键aid=?时专辑state变为2-已发布模式
            try (PreparedStatement ps=c.prepareStatement(sql)) {
                ps.setString(1,aid);
                ps.executeUpdate();

            }

        }
        resp.sendRedirect("http://127.0.0.1:8080/studio/album/list.html");
    }
}
